package HP.p0905;

import java.lang.reflect.Array;
import java.util.Arrays;

public class Test1 {
    public static void main(String[] args) {
        int a[] = {-5, -4, -2, -1, 1, 2, 2, 2, 2, 2, 3, 4, 6, 7, 7, 9, 9, 9, 9,};
        int aa = 8 >>> 3;
        System.out.println("a = " + aa);
        int b = binarySearch0(a, 0, a.length, -3);
        System.out.println("b = " + b);
        // System.out.println(Arrays.binarySearch(a, 8));
        ;
    }

    private static int binarySearch0(int[] a, int fromIndex, int toIndex,
                                     int key) {
        int low = fromIndex;
        int high = toIndex - 1;

        while (low <= high) {
            int mid = (low + high) >>> 1;
            int midVal = a[mid];

            if (midVal < key)
                low = mid + 1;
            else if (midVal > key)
                high = mid - 1;
            else
                return mid; // key found
        }
        return -(low + 1);  // key not found.
    }
}
